<template>
  <div class="app-container">

    <el-table ref="list" :height="tableHeight" v-loading="loading" :data="versionList">
      <el-table-column label="当前版本包名" align="center" prop="name" />
      <el-table-column label="当前版本号" align="center" prop="number" />
    </el-table>

    <el-row :gutter="10" class="mb5" justify="center" align="middle" style="display: flex; justify-content: center; align-items: center; margin-top: 60px">
      <el-col :span="3" style="display: flex; justify-content: center;">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="medium"
          @click="handleUpdate"
          v-hasPermi="['deyu:version:add']"
        >修改</el-button>
      </el-col>
    </el-row>

    <!-- 添加或修改版本包信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="当前版本包" prop="packageUrl">
          <file-upload v-model="form.packageUrl" :limit = "1" :fileType = "['vat']"
                       @upload-success="handleUploadSuccess"/>
        </el-form-item>
        <el-form-item label="当前版本号" prop="number">
          <el-input v-model="form.number" placeholder="请输入当前版本号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addVersion, delVersion, getVersion, listVersion, updateVersion} from "@/api/deyu/version";
import {format} from "echarts/lib/export";

export default {
  name: "Version",
  data() {
    return {
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 468,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 版本包信息表格数据
      versionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 1,
        orderByColumn: "create_time",
        isAsc: "desc",
        name: null,
        packageUrl: null,
        number: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "当前版本包名不能为空", trigger: "blur" }
        ],
        packageUrl: [
          { required: true, message: "当前版本包不能为空", trigger: "blur" }
        ],
        number: [
          { required: true, message: "当前版本号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询版本包信息列表 */
    getList() {
      this.loading = true;
      listVersion(this.queryParams).then(response => {
        this.versionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        packageUrl: null,
        number: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 单击行选中数据
    handleRowClick(row, column, event) {
      this.$refs.list.toggleRowSelection(row);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加版本包信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getVersion().then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改版本包信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let data = {
            "name": this.form.name,
            "packageUrl":this.form.packageUrl,
            "number":this.form.number,
          }
          addVersion(data).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function() {
        return delVersion(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleUploadSuccess(realName) {
      console.log("上传成功的文件名:", realName);
      this.form.name = realName;
    },
  }
};
</script>
